package com.maimai.service;

import com.maimai.dao.GoodsDao;
import com.maimai.dao.OrderDao;
import com.maimai.dao.OrderInfoDao;
import com.maimai.dao.UserDao;
import com.maimai.entity.Goods;
import com.maimai.entity.Order;
import com.maimai.entity.OrderInfo;
import com.maimai.entity.User;
import com.maimai.request.BuyInfo;
import com.maimai.request.BuyInfoDetail;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class OrderService {

    SqlSessionFactory factory;

    {
        //读取配置文件
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建session工厂
        factory = new SqlSessionFactoryBuilder().build(is);

    }


    public String buy(BuyInfo buyInfo){
        SqlSession session = factory.openSession(false);
        OrderInfoDao orderInfoDao = session.getMapper(OrderInfoDao.class);
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        OrderDao orderDao = session.getMapper(OrderDao.class);
        UserDao userDao = session.getMapper(UserDao.class);
        List<OrderInfo> infoList = new ArrayList<>();
        //转录数据
        for (BuyInfoDetail detail: buyInfo.getDetails()) {
            OrderInfo info = new OrderInfo();
            info.setGoodsId(detail.getGoodsId());
            info.setBuyNum(detail.getBuyNum());
            infoList.add(info);
        }
        //计算商品小计,计算总价
        Integer totalAmount = 0;
        for (OrderInfo info:infoList) {
            Goods goods = goodsDao.getOne(info.getGoodsId());
            int xiaoji = (int) (goods.getPrice() * info.getBuyNum());
            info.setAmount(xiaoji);
            totalAmount += xiaoji;
//            System.out.println(info);
        }
        //构造基础订单
        Order order = new Order();
        order.setUserId(buyInfo.getUserId());
        order.setAccount(userDao.getOne(order.getUserId(),null).getAccount());
        order.setCreateTime(new Date());
        order.setUserAddress(buyInfo.getAddress());
        order.setAmount(totalAmount);
        Random random = new Random();

        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String uuidstr = str.replace("-","").toUpperCase(Locale.ROOT);
        order.setOrderNo(uuidstr);
        System.out.println(order);
        //1.....插入基础订单
        orderDao.add(order);
        System.out.println("内部订单编号:" + order.getId());
        for (OrderInfo info:infoList) {
            info.setBaseOrderId(order.getId());
            //2.....插入订单明细
            orderInfoDao.add(info);
            //减少库存
            Goods goods = goodsDao.getOne(info.getGoodsId());
            if (info.getBuyNum() > goods.getStock()) {
                throw new RuntimeException("库存量不足");
            }
            goods.setStock(goods.getStock()- info.getBuyNum());
            //3.....修改商品库存
            goodsDao.update(goods);
        }
        session.commit();
        return order.getOrderNo();
    }
}
